package com.sunchangpeng.wechat.mp.stat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import lombok.Data;

import java.time.LocalDate;
import java.util.List;

public class MsgStats extends Stats {
    public static MsgStats with(MpAppSetting appSetting) {
        MsgStats stats = new MsgStats();
        stats.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return stats;
    }

    /**
     * 获取消息发送概况数据
     */
    public List<MsgSummary> getUpstreamMsg(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 7)) {
            throw new IllegalArgumentException("start和end相差不能超过7天以上");
        }

        String url = EndpointProvider.get("url.stats.msg.upstreammsg");
        return JSON.parseObject(doRequest(url, start, end), MsgSummaryWrapper.class).getList();
    }

    /**
     * 获取消息分送分时数据
     */
    public List<MsgSummary> getUpstreamMsgHour(LocalDate day) {
        String url = EndpointProvider.get("url.stats.msg.getupstreammsghour");
        return JSON.parseObject(doRequest(url, day, day), MsgSummaryWrapper.class).getList();
    }

    /**
     * 获取消息发送周数据
     */
    public List<MsgSummary> getUpstreamMsgWeek(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 30)) {
            throw new IllegalArgumentException("start和end相差不能超过30天以上");
        }

        String url = EndpointProvider.get("url.stats.msg.upstreammsgweek");
        return JSON.parseObject(doRequest(url, start, end), MsgSummaryWrapper.class).getList();
    }

    /**
     * 获取消息发送月数据
     */
    public List<MsgSummary> getUpstreamMsgMonth(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 30)) {
            throw new IllegalArgumentException("start和end相差不能超过30天以上");
        }

        String url = EndpointProvider.get("url.stats.msg.upstreammsgmonth");
        return JSON.parseObject(doRequest(url, start, end), MsgSummaryWrapper.class).getList();
    }

    /**
     * 获取消息发送分布数据
     */
    public List<MsgSummary> getUpstreamMsgDist(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 15)) {
            throw new IllegalArgumentException("start和end相差不能超过15天以上");
        }

        String url = EndpointProvider.get("url.stats.msg.upstreammsgdist");
        return JSON.parseObject(doRequest(url, start, end), MsgSummaryWrapper.class).getList();
    }

    /**
     * 获取消息发送分布周数据
     */
    public List<MsgSummary> getUpstreamMsgDistWeek(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 30)) {
            throw new IllegalArgumentException("start和end相差不能超过30天以上");
        }

        String url = EndpointProvider.get("url.stats.msg.upstreammsgdistweek");
        return JSON.parseObject(doRequest(url, start, end), MsgSummaryWrapper.class).getList();
    }

    /**
     * 获取消息发送分布月数据
     */
    public List<MsgSummary> getUpstreamMsgDistMonth(LocalDate start, LocalDate end) {
        if (diffThanDays(start, end, 30)) {
            throw new IllegalArgumentException("start和end相差不能超过30天以上");
        }

        String url = EndpointProvider.get("url.stats.msg.upstreammsgdistmonth");
        return JSON.parseObject(doRequest(url, start, end), MsgSummaryWrapper.class).getList();
    }

    @Data
    public static class MsgSummaryWrapper {
        private List<MsgSummary> list;
    }

    @Data
    public static class MsgSummary {
        @JSONField(name = "ref_date")
        private String refDate;

        @JSONField(name = "ref_hour")
        private Integer refHour;

        @JSONField(name = "msg_type")
        private Integer msgType;

        @JSONField(name = "msg_user")
        private Integer msgUser;

        @JSONField(name = "msg_count")
        private Integer msgCount;

        @JSONField(name = "count_interval")
        private Integer countInterval;

        @JSONField(name = "int_page_read_count")
        private Integer intPageReadCount;

        @JSONField(name = "ori_page_read_user")
        private Integer oriPageReadUser;
    }
}
